"""
__project__ = '官网项目'
__file_name__ = 'manage'
__author__ = 'wang'
__time__ = '2020/12/8 21:46'
__product_name = PyCharm
# !/usr/local/bin/python
# -*- coding:utf-8 -*-
"""

from flask import Flask, render_template, request
from sms import Submit
import pymysql
import logging
import os
import re

app = Flask(__name__)
random_num = None


def make_dir(make_dir_path):
    path = make_dir_path.strip()
    if not os.path.exists(path):
        os.makedirs(path)
    return path


@app.route('/commit')
def commit():
    conn = pymysql.connect(host='49.233.39.160', user='root', password='root@17Lebo', port=3306, database='lebo_OS')
    cur = conn.cursor()
    mobile = request.args['mobile']
    mobile_code = request.args['mobile_code']
    sql = "select * from register_os where register_phone=%s and password_register=%s;"
    num1 = cur.execute(sql, [mobile, mobile_code])
    select_01 = str(cur.fetchone())

    if num1 == 1:
        result = {'token': random_num, 'result': select_01}
        app.logger.info('查询到结果，可以看视频', mobile)
        return render_template('login_video.html')
    else:
        ret = re.match(r"^(0|86|17951)?(13[0-9]|15[012356789]|17[0-8]|18[0-9]|14[5679]|19[189]|16[56])[0-9]{8}$",
                       mobile)
        ret_code = re.match(r"^(\d{4})|(\d{6})$", mobile_code)

        if ret and ret_code:
            sql1 = "insert INTO register_os(register_phone, password_register) values(%s, %s);"
            cur.execute(sql1, [mobile, mobile_code])
            conn.commit()
            app.logger.info('执行成功，增加数据', mobile)
            return render_template('login_video.html')
        else:
            app.logger.info('没有查到结果，返回登录页面', mobile)
            return render_template('login.html')


# def phonecheck(s):
#     # 号码前缀，如果运营商启用新的号段，只需要在此列表将新的号段加上即可。
#     phoneprefix = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '150', '151', '152', '153',
#                    '156', '158', '159', '176', '170', '183', '182', '185', '186', '188', '189']
#     # 检测号码是否长度是否合法。
#     if len(s) != 11:
#         print("The length of phonenum is 11.")
#     else:
#         # 检测输入的号码是否全部是数字。
#         if s.isdigit():
#             # 检测前缀是否是正确。
#             if s[:3] in phoneprefix:
#                 Submit(mobile)
#                 print("The phone num is valid.")
#             else:
#                 print("The phone num is invalid.")
#         else:
#             print("The phone num is made up of digits.")


@app.route('/login')
def index_login():
    app.logger.info('显示login内容')
    return render_template('login.html')


@app.route('/course.html')
def index_course():
    app.logger.info('显示课程内容')
    return render_template('course.html')


@app.route("/sms_send", methods=['POST', 'GET'])
def sms_send():
    mobile = request.args['mobile']
    Submit(mobile)
    app.logger.info('发送验证码成功,发送手机号为：', mobile)
    return {'code': 200}


@app.route('/')
def index_mmmmm():
    app.logger.info('显示首页index内容')
    return render_template('index.html')


@app.route('/index_new')
def index_new():
    app.logger.info('登录后的index页面')
    return render_template('index_new.html')


@app.route('/login_video')
def login_video():
    app.logger.info('登录后链接视频')
    return render_template('login_video.html')


@app.route('/login_video1')
def login_video1():
    app.logger.info('登录后链接视频')
    return render_template('login_video1.html')


logging.basicConfig(level=logging.DEBUG,  # 控制台打印的日志级别
                    filename=
                    'log_new.log',  # 将日志写入log_new.log文件中
                    filemode='a',  # 模式，有w和a，w就是写模式，每次都会重新写日志，覆盖之前的日志
                    # a是追加模式，默认如果不写的话，就是追加模式
                    format=
                    '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                    # 日志格式
                    )

app.run(debug=True, host='0.0.0.0')
